import requests
from bs4 import BeautifulSoup
import re
import csv

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}

url = 'https://money.163.com/'

response = requests.get(url, headers=headers)
# print(response.text)
soup = BeautifulSoup(response.text, "html.parser")
# print(soup.prettify())
tag1 = soup.select('.tab_content h2>a')
tag2 = soup.select('.tab_content h3>a')
tag = tag1 + tag2
# print(tag)
all_news = []
for i in tag:
    title = i.get_text()
    link = i.get('href')    
    news = {'标题':title, '链接':link}
    # print(news)
    all_news.append(news)
# print(all_news)

with open('网易财经的实时新闻.csv', 'w', encoding='utf-8-sig', newline='') as f:
    fiednames = ['标题', '链接']
    writer = csv.DictWriter(f,fieldnames=fiednames)
    writer.writeheader()
    writer.writerows(all_news)

    # writer = csv.writer(f)
    # writer.writerow(['标题', '链接'])
    # for news in all_news:
    #     writer.writerow([news['标题'], news['链接']])